import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('../components/Login.vue')
  },
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/home',
    name: 'Home',
    component: () => import('../components/Home.vue'),
    redirect: '/welcome',
    children: [
      {
        path: '/welcome',
        component: () => import('../components/Welcome.vue')
      },
      {
        path: '/users',
        component: () => import('../components/users/Users.vue')
      },
      {
        path: '/rights',
        component: () => import('../components/rights/Rights.vue')
      },
      {
        path: '/roles',
        component: () => import('../components/rights/Roles.vue')
      },
      {
        path: '/goods',
        component: () => import('../components/goods/Goods.vue')
      },
      {
        path: '/params',
        component: () => import('../components/goods/Params.vue')
      },
      {
        path: '/categories',
        component: () => import('../components/goods/Categories.vue')
      },
      {
        path: '/orders',
        component: () => import('../components/orders/Orders.vue')
      },
      {
        path: '/reports',
        component: () => import('../components/reports/Reports.vue')
      },
      {
        path: '/addgoods',
        component: () => import('../components/goods/AddGoods.vue')
      }
    ]
  }
]

const router = new VueRouter({
  mode: 'hash',
  base: process.env.BASE_URL,
  routes
})
router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  //获取token
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
});

export default router
